<?php
namespace app\home\controller;
session_start();
use think\Db;
use think\captcha\Captcha;
use think\Loader;
class Reg extends \think\Controller{
    public function reg(){
        // $pid=$_GET['id'];
        // $key=$_GET['key'];
        if(isset($_POST['up'])){
            $username=input('param.username','','htmlspecialchars');
            $tell=input('param.tell','','htmlspecialchars');
            $tellCode=input('param.tellCode','','htmlspecialchars');
            $pwd=input('param.pwd','','htmlspecialchars');
            $pwd2=input('param.pwd2','','htmlspecialchars');
            $email=input('param.email','','htmlspecialchars');
            $pid=$_GET['id'];
            $key=$_GET['key'];
        $data=[
            'username'=>$username,
            'tell'=>$tell,
            'pwd'=>$pwd,
            'pwd2'=>$pwd2,
            'email'=>$email,
        ];       //添加一条数据
        // $validate	=	Loader::validate('User');
        // $result=$validate->check($data);
        // if($result==false){
        //     $this->error($validate->getError());
        // }
        if($tellCode!=$_SESSION['code']){
            $this->error('短信验证错误!');
        }
        if($tellCode==$_SESSION['code']){           //$result==true&&
            $data=['username'=>$username,'tell'=>$_SESSION['tell'],'pwd'=>$pwd,'email'=>$email, 'pid'=>$pid,'distribution'=>'0'];
            $ret=Db::name('user')->insertGetId($data);
            if($ret>0){
                $this->success('注册成功!','home/login/login');
                session('user_id',$ret['id']);
            }else {$this->error('注册失败!'); 
                }    
            }
        }
        return $this->fetch('reg');//继承基类的控制器 
    }

    public function sms(){
        $tell=input('param.tell','','htmlspecialchars');
        $tellCode=mt_rand(100000,999999);
        $_SESSION['code']=$tellCode;
        $_SESSION['tell']=$tell;
        Vendor('topthink.aliyun.sendSms');
        $ret=sendSms($tell,$tellCode);
        echo '成功';
    }
    
}